#include "StdAfx.h"
#include <OpenCVLib.h>
#include "FilyOpenCV.h"


CFilyOpenCV::CFilyOpenCV(void)
{
}


CFilyOpenCV::~CFilyOpenCV(void)
{
}

	// GetImageROI
void CFilyOpenCV::GetImageROI(IplImage* src, IplImage* mask, IplImage* desc)
{
	if(src->depth != desc->depth)
	{
		return;
	}
	if(src->nChannels != desc->nChannels)
	{
		return;
	}
	if((src->width != desc->width)||(src->height != desc->height))
	{
		return;
	}
	int i = 0;
	int j = 0;
	int m = 0;
	double data = 0.0;
	const uchar *maskdata = (const uchar *) mask->imageData;
	const uchar *srcdata = (const uchar *) src->imageData;
	uchar *descdata = (uchar *) desc->imageData;
	//for(i = 0 ; i < 2; ++i)
	//{
	//	//++maskdata;
	//	//++maskdata;
	//	for(int j=0; j<mask->width; j++)  
	//	{
	//		++maskdata;
	//	}  
	//	maskdata += mask->widthStep-mask->width;
	//}
	for(i = 0 ; i < src->height; ++i)
	{
		//++maskdata;
		//++maskdata;
		//maskdata +=10;
		for(int j=0; j<src->width; j++)  
		{  
			data = maskdata[0];
			//data = *(mask->imageData + (i+2)*mask->widthStep + j+2);
			if(data > 100)
			{
				for(int m = 0 ; m < src->nChannels; ++m)
				{
					descdata[m] = srcdata[m];
				}				
				//descdata[1] = srcdata[1];
				//descdata[2] = srcdata[2];
			}
			else
			{
				data = srcdata[0];
			}
			srcdata += src->nChannels;
			descdata += src->nChannels;
			++maskdata;
		}  
		maskdata += mask->widthStep-mask->width;
		srcdata += src->widthStep-src->nChannels*src->width;
		descdata += desc->widthStep-desc->nChannels*desc->width;
	}
}

	// GetImageROI
void CFilyOpenCV::GetImageROI(IplImage* src, CvScalar color,int difference,  IplImage* desc)
{
	CvScalar find; 
	double select = (color.val[0] * 255 + color.val[1]) * 255 + color.val[2];
	int bleft = select - difference;
	int bright = select + difference;
	GetPointByColor(color,bright,bleft,difference);
	double bfind = (color.val[0] * 255 + color.val[1]) * 255 + color.val[2];
	int itype = 0;
	for(int i = 0 ; i < src->height; ++i)
	{
		for(int j=0; j<src->width; j++)  
		{  
			find = cvGet2D(src,i,j);
			//bfind = (find.val[0] * 255 + find.val[1]) * 255 + find.val[2];
			//if((bfind>bleft) && (bfind<bright))
			//{
			//	//cvSet2D(desc,i,j,find);
			//}
			//else
			//{
			//	cvSet2D(desc,i,j,find);
			//}
			itype = GetPointByColor(find,difference);
			if(itype != 0)
			{
				cvSet2D(desc,i,j,find);
			}
		}  

	}
}


	// GetImageROI
void CFilyOpenCV::GetImageROI(IplImage* src, CvPoint point,int difference, IplImage* desc)
{
	CvScalar sel = cvGet2D(src,point.y,point.x);
	GetImageROI(src,sel,difference,desc);
}


	// GetImageROI
void CFilyOpenCV::GetPointByColor(CvScalar color,int &max,int &min,int difference)
{
	BYTE rgb[3] = {0,0,0};
	rgb[0] = color.val[0];
	rgb[1] = color.val[1];
	rgb[2] = color.val[2];
	int i = 0;
	BYTE bmax = rgb[0];
	if(bmax < rgb[1])
	{
		i = 1;
		bmax = rgb[1];
	}
	if(bmax < rgb[2])
	{
		i = 2;
	}
	bmax = rgb[i];
	rgb[i] += difference;
	max = (rgb[0] * 255 + rgb[1]) * 255 + rgb[2];
	rgb[i] = bmax;
	rgb[i] -= difference;
	min = (rgb[0] * 255 + rgb[1]) * 255 + rgb[2];
}


	// GetImageROI
int CFilyOpenCV::GetPointByColor(CvScalar color,int difference)
{
	BYTE rgb[3] = {0,0,0};
	rgb[0] = color.val[0];
	rgb[1] = color.val[1];
	rgb[2] = color.val[2];
	int i = 0;
	BYTE bmax = rgb[0];
	if(bmax < rgb[1])
	{
		i = 1;
		bmax = rgb[1];
	}
	if(bmax < rgb[2])
	{
		i = 2;
	}
	difference = 255 - difference;
	if( ((rgb[0]-rgb[1])> difference) || ((rgb[0]-rgb[2])> difference))
	{
		i = 0;
	}
	else
	{
		i = 1;
	}
	return i;
}


// GetImageROI
CvScalar CFilyOpenCV::GetImageByPoint(IplImage* src, CvPoint point)
{
	double data[3] = {0.0,0.0,0.0};
	const uchar *srcdata = (const uchar *) src->imageData;
	srcdata += src->widthStep*(point.y-1) + src->nChannels*point.x;
	for(int m = 0 ; m < src->nChannels; ++m)
	{
		data[m] = srcdata[m];
	}	
	CvScalar s;
	s = cvGet2D(src,point.y,point.x);

	return CV_RGB( data[0], data[1], data[2] );
}